package slxy.jiangtao.dts.core.common.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import java.util.concurrent.TimeUnit;

/**
 * Redis工具 Created by Gordon on 2016/11/30.
 */
/*@Repository("redisKit")*/
public final class RedisKit {
    RedisTemplate<String, String> redisTemplate;

    private ValueOperations<String, String> opsForValue;


    public void setRedisTemplate(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    /**
     * 设置key-value到Redis, 含过期的时间方式
     *
     * @param k
     * @param t
     * @param period
     * @param timeUnit
     */
    public void setValue(String k, String t, Long period, TimeUnit timeUnit) {
        if (this.opsForValue == null) {
            opsForValue = redisTemplate.opsForValue();
        }
        opsForValue.set(k, t, period, timeUnit);
    }

    /**
     * 设置key-value到Redis
     *
     * @param key
     * @param value
     */
    public void setValue(String key, String value) {
        opsForValue.set(key, value);
    }

    /**
     * 从Redis中取出值
     *
     * @param key
     * @return
     */
    public String getValue(String key) {
        return opsForValue.get(key);
    }

}
